class MyPromise {
  static PENDING = 'pending'
  static FULFILLED = 'fulfilled'
  static REJECTED = 'rejected'
  constructor(func) {
    this.PromiseState = MyPromise.PENDING
    this.PromiseResult = null
    try {
      func(this.resolve.bind(this), this.reject.bind(this))
    } catch (error) {
      this.reject(error)
    }
  }
  resolve(result) {
    if (this.PromiseState === MyPromise.PENDING) {
      this.PromiseState = MyPromise.FULFILLED
      this.PromiseResult = result
    }
  }
  reject(reason) {
    if (this.PromiseState === MyPromise.PENDING) {
      this.PromiseState = MyPromise.REJECTED
      this.PromiseResult = reason
    }
  }
  then(onFulfilled, onRejected) {
    const fulfilled = typeof onFulfilled === 'function' ? onFulfilled : (onFulfilled) => onFulfilled
    const rejected = typeof onRejected === 'function' ? onRejected : (onRejected) => {
      throw onRejected
    }
    if (this.PromiseState === MyPromise.FULFILLED) {
      setTimeout(() => fulfilled(this.PromiseResult))
    }
    if (this.PromiseState === MyPromise.REJECTED) {
      setTimeout(() => rejected(this.PromiseResult))
    }
  }
}
console.log(1)
const promise = new MyPromise((resolve, reject) => {
  // resolve('123')
  console.log(2)
  reject(3)
})
promise.then(null, (result) => {
  console.log(result)
})
console.log(4)